package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ParkOperatorUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ParkOperatorUploadResponse;
import io.renren.modules.basedata.dao.ParkOperatorUploadDao;
import io.renren.modules.basedata.entity.tbl_ParkOperator;
import io.renren.modules.basedata.service.ParkOperatorUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ParkOperatorUploadServiceImpl implements ParkOperatorUploadService {

    private static final Logger log = LoggerFactory.getLogger(ParkOperatorUploadServiceImpl.class);

    @Autowired
    private ParkOperatorUploadDao parkOperatorUploadDao;

    @Override
    public String ParkOperatorUpload() throws Exception {
        log.info("开始获取停车场ETC运营方信息！");
        int operResult = 0;
        List<tbl_ParkOperator> list = null;
        tbl_ParkOperator ParkOperator = null;
        //获取停车场信息
        list = getParkOperatorList();
        if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                ParkOperator = list.get(i);
                //停车场ETC运营方信息上传及变更接口
                ParkOperatorUploadRequest request = new ParkOperatorUploadRequest();
                request.setId(ParkOperator.getId());
                request.setName(ParkOperator.getName());
                request.setContact(ParkOperator.getContact());
                request.setTel(ParkOperator.getTel());
                request.setAddress(ParkOperator.getAddress());
                request.setOperation(ParkOperator.getFlag());
                //获取发送文件名
                String fileName = ETCCommHelper.getFileName(request, "65");
                //生成就json文件
//				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);
                try{
                    //指定响应的协议模型,调用upload发送数据
                    ParkOperatorUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, ParkOperatorUploadResponse.class);
                    log.info("上传停车场ETC运营方信息成功,服务器返回:"+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){
                        //更新上传标志
                        if(parkOperatorUploadDao.updateParkOperator(ParkOperator.getId())==1){
                            log.info("更新停车场ETC运营方信息表上传标志成功!");
                            operResult = 1;
                        }else{
                            log.info("更新停车场ETC运营方信息表表上传标志成功!");
                        }
                    }else{
                        log.info("上传停车场ETC运营方信息失败!失败原因:"+response.getInfo());

                        parkOperatorUploadDao.updateParkOperatorError(ParkOperator.getId());
                    }
                }catch(IOException e){
                    log.error("上传停车场ETC运营方信息失败！错误信息:"+e.getMessage());
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传停车场ETC运营方信息失败！错误信息:"+apie.getMessage());

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        parkOperatorUploadDao.updateParkOperatorError(ParkOperator.getId());
                    }
                    continue;
                }
            }
            return "上传停车场ETC运营方信息结束！";
        }else{
            return "无停车场ETC运营方信息,上传结束...";
        }
    }

    //获取停车场ETC运营方信息
    @SuppressWarnings("unchecked")
    public List<tbl_ParkOperator> getParkOperatorList() throws IOException{
        List<tbl_ParkOperator> list;
        try{
            list = parkOperatorUploadDao.getParkOperatorList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取停车场ETC运营方信息信息失败！错误信息:",e);
            throw new IOException("获取停车场ETC运营方信息失败:"+e.getMessage());
        }
        return list;
    }
}
